package com.alt.processor;

import java.util.Collections;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import com.alt.connector.RestClient;
import com.alt.models.AdsInfo;
import com.alt.models.ImageModel;
import com.alt.models.TransactionData;
import com.alt.models.UserData;
import com.alt.models.UserData2;
import com.alt.utils.ALTSysUtils;

import android.util.Log;

public class CMProcessor extends BaseModuleProcessor {

	private final static String URL_VALIDATE_USER = "mobile/login/%s&%s&%s&%s&%s?format=json";	
	private final static String URL_CHECK_PUT_METHOD = "mobile/checkputmethod/?format=json";	
	
	private final static String URL_GET_ADS_LIST = "common/ads/list?format=json";	
	private final static String URL_GET_ADS_LIST_BY_USERID = "common/ads/list/%s&%s?format=json";	
	private final static String URL_GET_ADS_LIST_IMAGE = "common/ads/getimgs/%s?format=json";
	
	private final static String URL_UPDATE_PROFILE_PICTURE = "common/image/save/?format=json";	
	
	private final static String URL_UPDATE_TRANSACTION = "common/transaction/update/?format=json";	
	
	private final static String URL_GET_TRANSACTION_lIST_BY_USERID = "common/transaction/list/%s?format=json";	
	
	private final static String URL_REGISTER_NEW_ACCOUNT = "mobile/register/%s&%s&%s?format=json";	

	// User Mobile Login
	// <returns>Success | InvalidUserName | InvalidPassword | IsLockedOut |
	// IsNotApproved | UnknownError</returns>
	public UserData mobilelogin(String username, String password, String deviceid, String deviceip,String accounttype) {
		HttpEntity<?> requestEntity = createGETRequestEntity();
		UserData res = new UserData();
		try{
			RestClient client = new RestClient();
			String url = ALTSysUtils.format(URL_VALIDATE_USER, username,password,deviceid,deviceip,accounttype);
	
			ResponseEntity<UserData> response = client.template().exchange(RestClient.apiUrl(url), HttpMethod.GET, requestEntity,UserData.class);			
			res = response.getBody();
		} catch (Exception ex) {
			Log.i("AdlatteMobileLogin", ""+ex.getMessage());
		}		
		return res;
	}
	
	public UserData CheckPutMethod(UserData userdata) {
		HttpHeaders requestHeaders = new HttpHeaders();
		requestHeaders.setAccept(Collections.singletonList(new MediaType("application", "json")));
		HttpEntity<UserData> requestEntity = new HttpEntity<UserData>(userdata, requestHeaders);
		RestClient client = new RestClient();
		String url = String.format(URL_CHECK_PUT_METHOD);	
				
		UserData res = null;
		try{		
			ResponseEntity<UserData> response = client.template().exchange(RestClient.apiUrl(url), HttpMethod.PUT, requestEntity,UserData.class);
					res = response.getBody();
		} catch (Exception ex) {
				Log.i("CheckPutMethod", ex.getMessage());
		}
			Log.i("CheckPutMethod", res.toString());
		return res;
	}	
	
	public AdsInfo[] getAdsList() {
		HttpEntity<?> requestEntity = createGETRequestEntity();
		AdsInfo[] res = null;
		try{ 
			RestClient client = new RestClient();
			String url = ALTSysUtils.format(URL_GET_ADS_LIST);
	
			ResponseEntity<AdsInfo[]> response = client.template().exchange(RestClient.apiUrl(url), HttpMethod.GET, requestEntity,AdsInfo[].class);			
			res = response.getBody();
		} catch (Exception ex) {
			Log.i("getAdsList", ex.getMessage());
		}		
		return res;
	}
	
	public AdsInfo[] getAdsListByUserid(String userid,String loadeditemcount) {
		HttpEntity<?> requestEntity = createGETRequestEntity();
		AdsInfo[] res = null;
		try{ 
			RestClient client = new RestClient();
			String url = ALTSysUtils.format(URL_GET_ADS_LIST_BY_USERID, userid,loadeditemcount);
	
			ResponseEntity<AdsInfo[]> response = client.template().exchange(RestClient.apiUrl(url), HttpMethod.GET, requestEntity,AdsInfo[].class);
			res = response.getBody();
		} catch (Exception ex) {
			Log.i("getAdsListByUserid", ex.getMessage());
		}		
		return res;
	}
	
	public ImageModel getAdsListImage(String pos) {
		HttpEntity<?> requestEntity = createGETRequestEntity();
		ImageModel res = null;
		try{ 
			RestClient client = new RestClient();
			String url = ALTSysUtils.format(URL_GET_ADS_LIST_IMAGE,pos);			
			ResponseEntity<ImageModel> response = client.template().exchange(RestClient.apiUrl(url), HttpMethod.GET, requestEntity,ImageModel.class);			
			res = (ImageModel)response.getBody();			
		} catch (Exception ex) {
			Log.i("getAdsListImage", ex.getMessage());
		}		
		return res;
	}
	
	public String updateUserProfile(UserData2 uData){
		HttpHeaders requestHeaders = new HttpHeaders();
		requestHeaders.setAccept(Collections.singletonList(new MediaType("application", "json")));
		HttpEntity<UserData2> requestEntity = new HttpEntity<UserData2>(uData, requestHeaders);
		RestClient client = new RestClient();
		String url = String.format(URL_UPDATE_PROFILE_PICTURE);	
				
		String res = null;
		try{		
			Log.v("HAI", "1");
			ResponseEntity<String> response = client.template().exchange(RestClient.apiUrl(url), HttpMethod.PUT, requestEntity,String.class);
			Log.v("HAI", "2 "+response);
            res = response.getBody();
		} catch (Exception ex) {
				Log.i("updateUserProfile", ex.getMessage());
		}
			Log.i("updateUserProfile", res.toString());
		return res;
	}	
	
	public String updateTransaction(TransactionData trans){
		HttpHeaders requestHeaders = new HttpHeaders();
		requestHeaders.setAccept(Collections.singletonList(new MediaType("application", "json")));
		HttpEntity<TransactionData> requestEntity = new HttpEntity<TransactionData>(trans, requestHeaders);
		RestClient client = new RestClient();
		String url = String.format(URL_UPDATE_TRANSACTION);	
				
		String res = null;
		try{		
			ResponseEntity<String> response = client.template().exchange(RestClient.apiUrl(url), HttpMethod.PUT, requestEntity,String.class);
            res = response.getBody();
		} catch (Exception ex) {
				Log.i("updateTransaction", ex.getMessage());
		}
			Log.i("updateTransaction", res.toString());
		return res;
	}	
	
	public TransactionData[] getTransactionByUserId(String userid) {
		HttpEntity<?> requestEntity = createGETRequestEntity();
		TransactionData[] res = null;
		try{ 
			RestClient client = new RestClient();
			String url = ALTSysUtils.format(URL_GET_TRANSACTION_lIST_BY_USERID, userid);	
			ResponseEntity<TransactionData[]> response = client.template().exchange(RestClient.apiUrl(url), HttpMethod.GET, requestEntity,TransactionData[].class);
			res = response.getBody();
		} catch (Exception ex) {
			Log.i("getTransactionByUserId", ex.getMessage());
		}		
		return res;
	}
	
	public String mobileregister(String username,String password,String deviceid) {
		HttpEntity<?> requestEntity = createGETRequestEntity();
		String res = null;
		try{ 
			RestClient client = new RestClient();
			String url = ALTSysUtils.format(URL_REGISTER_NEW_ACCOUNT, username,password,deviceid);
	
		ResponseEntity<String> response = client.template().exchange(RestClient.apiUrl(url), HttpMethod.GET, requestEntity,String.class);
			res = response.getBody();
		} catch (Exception ex) {
			Log.i("mobileregister", ex.getMessage());
		}		
		return res;
	}
}
